import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-10-23
 * Time: 21:47
 */
public class TcpEchoClient {
    public Socket socket = null;
    public TcpEchoClient(String serverIp, int serverPort) throws IOException {
        //这里需要服务器的IP和端口才能和服务器建立连接
        socket = new Socket(serverIp, serverPort);
    }
    public void start() throws IOException {
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            Scanner scannerNet = new Scanner(inputStream);
            PrintWriter writer = new PrintWriter(outputStream)){
                while(true) {
                    System.out.println("请输入要发送的内容");
                    String request = scanner.next();
                    writer.println(request);
                    writer.flush();
                    String response = scannerNet.next();
                    System.out.println(response);
                }
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient = new TcpEchoClient("127.0.0.1", 9090);
        tcpEchoClient.start();
    }
}
